import requests

from fake_useragent import UserAgent
import os
import re
headers = {
    "User-Agent": UserAgent().random,
    "Referer": "https://music.163.com",  # 设置防盗链
}
os.makedirs("网易云音乐", exist_ok=True)
url=input("请输入要下载的网易云音乐链接：(Vip音乐暂时无法下载)\n")
id=re.findall(r'song\?id=(.*?)\&',url)[0]
print(id)
download_url=f'http://music.163.com/song/media/outer/url?id={id}'
rsp_down=requests.get(url=download_url,headers=headers).content

rsp=requests.get(url,headers=headers)
name=re.findall(r'\<title>(.*)\<\/title>',rsp.text)[0]
print(f'{name}.mp3')
with open (f'网易云音乐/{name}.mp3',mode="wb") as f:
    f.write(rsp_down)